// 在classList筛选出所有大类

import dayjs from "dayjs"

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function separateClass(data: any, type: string): any {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let bigClass: string | any[] = []
  if(type === 'pay') {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    bigClass = data.filter((item: any) => {
      return (item.sub === false && item.type === 'pay')
    })
  } else if(type === 'income') {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    bigClass = data.filter((item: any) => {
      return (item.sub === false && item.type === 'income')
    })
  }
  console.log(bigClass, 'helper,', type);
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const smallClass = data.filter((item: any) => {
    return item.sub === true
  })
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  smallClass.forEach((item: any) => {
    for(let i =0; i<bigClass.length; i++) {
      bigClass[i].children = bigClass[i].children ||  []
      if(bigClass[i].id === item.fromId) {
        bigClass[i].children.push(item)
      }
    }
  });
  return bigClass
}

// 通过titleId获取类名
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
export function getClassTitle(isSub: boolean, id: number, data: any, icon?: boolean): string {
  let name = ''
  let iconName = ''
  if(isSub) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    data.forEach((item: any) => {
      if(item.sub === true && item.id === id) {
        name = item.name
        iconName = item.icon
      }
    });
    return icon ? iconName : name
  } else  {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    data.forEach((item: any) => {
      if(item.sub === false && item.id === id) {
        name = item.name
        iconName = item.icon
      }
    });
    return icon ? iconName : name
  }
}

// 设置日历组件的可选日期范围，默认为前三个月到今天
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setCalendarShowData(showMonth: number): any {
  const date = new Date()
  const day = date.getDate()
  let month = date.getMonth() + 1
  let year = date.getFullYear()
  const showData = {
    start: new Date(),
    end: new Date()
  }
  showData.end = new Date(year, month, day)
  // 当前月份 - 可展示月份 < 1  0或负数
  if (month - showMonth < 1) {
    year--
    // 总月数 - （ 可展示月份 - 1）
    month = 12 - (showMonth - month)
  } else {
    month = month - showMonth
    month--
  }
  showData.start = new Date(year, month - 1, day)
  return showData
}


// 通过月份获取一个月的天数
export function getMonthCount(month: number, year: number): number {
  let count = 0
  switch (month) {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
      count = 31
      break;
    case 4: case 6: case 9: case 11:
      count = 30
      break;
    case 2: 
      if((year%4==0 && year%100!=0)||(year%400==0)){
        count = 29
      } else {
        count = 28
      }
      break;
    default:
      break;
  }
  return count
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function handleGetFormatList (data: any, start: Date, count: number): any  {
  const moneyList = []
  for (let i = 0; i < count; i++) {
    const date  = dayjs(start.getTime() + i * 86400000).format('YYYY-MM-DD')
    let p: { income: number; pay: number }[] =[]
    if(data) {
      p = data.filter((item: { date: unknown }) => {
        return item.date === date
      })
    }
    if(p[0]) {
      const { income, pay } = p[0]
      moneyList.push({income, pay, date})
    } else {
      moneyList.push({income: 0, pay: 0, date})
    }
  }
  return moneyList
}

export function getStartAndEnd (date: Date): {start: Date, end: Date} {
  const result = {
    start: new Date(),
    end: new Date()
  }
  const year = date.getFullYear()
  const month = date.getMonth()
  const count = getMonthCount(month + 1, year)
  result.start = new Date(year, month, 1)
  result.end = new Date(year, month, count)
  return result
}